package com.example.shopproject.controller;



import com.example.shopproject.Repository.CommentListRepository;
import com.example.shopproject.Repository.CommentRepository;
import com.example.shopproject.Repository.CommentSetRepository;
import com.example.shopproject.entity.Comment;
import com.example.shopproject.entity.CommentList;
import com.example.shopproject.entity.CommentSet;
import com.example.shopproject.entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


@RestController
@RequestMapping("/comment")
public class CommentController {

    private final CommentListRepository commentListRepository;
    private final CommentRepository commentRepository;
    private final CommentSetRepository commentSetRepository;


    LocalDateTime now = LocalDateTime.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
    String timenow = now.format(formatter);


    @Autowired
    public CommentController(CommentListRepository commentListRepository, CommentRepository commentRepository, CommentSetRepository commentSetRepository) {
        this.commentListRepository = commentListRepository;

        this.commentRepository = commentRepository;
        this.commentSetRepository = commentSetRepository;
    }


//    查询所有评论
    @GetMapping("/listall")
    public Result<?> getAllcomment() {

        List<CommentList> comments= commentListRepository.findByCommentLevel(1);
        System.out.println("调用/listall成功");
        return Result.success(comments);
    }



    //    根据商品查询评论
    @RequestMapping("/getcomment/{CommentId}")
    public Result<?> getCommentByShopId(@PathVariable Integer CommentId) {

    Comment comment = commentRepository.findBycommentId(CommentId);
    return Result.success(comment);

    }


    @RequestMapping("/setcomment")
    public Result<?> setcomment(@RequestBody CommentSet commentSet) {

            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String timenow = now.format(formatter);
        commentSet.setCommentTime(timenow);
        commentSet.setCreateTime(timenow);
        commentSet.setUpdateTime(timenow);
        commentSet.setCommentLevel(1);

        commentSetRepository.save(commentSet);

        return Result.success();
    }

    public void update(CommentSet commentSet) {
        CommentSet oldcommentset = commentSetRepository.findBycommentId(commentSet.getCommentId());

        if (commentSet.getCommentLevel()==null){
            commentSet.setCommentLevel(oldcommentset.getCommentLevel());
        }
        if (commentSet.getCommentTime()==null){
            commentSet.setCommentTime(oldcommentset.getCommentTime());
        }
        if (commentSet.getUserId()==null){
            commentSet.setUserId(oldcommentset.getUserId());
        }
        if (commentSet.getCommentText()==null){
            commentSet.setCommentText(oldcommentset.getCommentText());
        }
        if (commentSet.getCommentLike()==null){
            commentSet.setCommentLike(oldcommentset.getCommentLike());
        }
        if (commentSet.getCommentGrade()==null){
            commentSet.setCommentGrade(oldcommentset.getCommentGrade());
        }
        if (commentSet.getCreateTime()==null){
            commentSet.setCreateTime(oldcommentset.getCreateTime());
        }
        if (commentSet.getUpdateTime()==null){
            commentSet.setUpdateTime(oldcommentset.getUpdateTime());
        }
        if (commentSet.getCommentPicture()==null){
            commentSet.setCommentPicture(oldcommentset.getCommentPicture());
        }
        if (commentSet.getShopComment()==null){
            commentSet.setShopComment(oldcommentset.getShopComment());
        }
        if (commentSet.getCommodityId()==null){
            commentSet.setCommodityId(oldcommentset.getCommodityId());
        }
        if (commentSet.getUserComment()==null){
            commentSet.setUserComment(oldcommentset.getUserComment());
        }
        if (commentSet.getShopId()==null){
            commentSet.setShopId(oldcommentset.getShopId());
        }

        commentSetRepository.save(commentSet);

    }


    @RequestMapping("/setusercomment/{CommentId}")
    public Result<?> setusercomment(@RequestBody CommentSet commentSet,@PathVariable int CommentId) {


        commentSet.setCommentTime(timenow);
        commentSet.setCreateTime(timenow);
        commentSet.setUpdateTime(timenow);
        commentSet.setCommentLevel(2);

        CommentSet commentsetpa = new CommentSet() ;
        commentsetpa.setCommentId(CommentId);

        commentSet=commentSetRepository.save(commentSet);


        commentsetpa.setUserComment(commentSet.getCommentId());
        commentsetpa.setUpdateTime(timenow);

        update(commentsetpa);


        return Result.success();

    }

    @RequestMapping("/setshopcomment/{CommentId}")
    public Result<?> setshopcomment(@RequestBody CommentSet commentSet,@PathVariable int CommentId) {


        commentSet.setCommentTime(timenow);
        commentSet.setCreateTime(timenow);
        commentSet.setUpdateTime(timenow);
        commentSet.setCommentLevel(2);

        CommentSet commentsetpa = new CommentSet() ;
        commentsetpa.setCommentId(CommentId);

        commentSet=commentSetRepository.save(commentSet);
        System.out.println("id是"+commentSet.getCommentId());


        commentsetpa.setShopComment(commentSet.getCommentId());
        commentsetpa.setUpdateTime(timenow);

        update(commentsetpa);

        return Result.success();

    }

        @RequestMapping("/delete/{commentId}")
    public Result<?> deleteCommentById(@PathVariable int commentId) {


        commentSetRepository.deleteById(commentId);

        return Result.success();

    }


    @RequestMapping("/update")
    public Result<?> updateComment(@RequestBody CommentSet commentSet) {


        update(commentSet);

        return Result.success();

    }

}
